package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-07
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public void updateReservationStatus() {
        //查询预约时间过期的
        List<Reservation> list = lambdaQuery().eq(Reservation::getStatus, 0).lt(Reservation::getTime, LocalDateTime.now()).list();
        if(CollUtil.isNotEmpty(list)){
            //获取所有的ID,并且转换为集合
            List<Long> ids = list.stream().map(Reservation::getId).collect(Collectors.toList());
            //批量更新
            reservationMapper.batchUpdateStatus(ids);
        }
    }

    /**
     * 查询当天取消预约数量
     * @return
     */
    @Override
    public Integer cancelreservationCount() {
        //获取今天的时间startTime-endTime:如26号零点到27点
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //拿到当前用户id
        Long id=UserThreadLocal.get();
        return reservationMapper.cancelreservationCount(startTime,endTime,id);
    }

    /**
     * 查询每个时间段内剩余预约次数
     * @return
     */
    @Override
    public List<TimeCountVo>  reservationCount() {
        return reservationMapper.reservationCount();
    }

    /**
     * 新增预约数据
     * @param dto
     */
    @Override
    public void saveReservation(ReservationDto dto) {
        //拿到当前用户id
        Long id=UserThreadLocal.get();
        Reservation reservation=new Reservation();
        //开始搬砖
        reservation.setTime(dto.getTime());
        reservation.setVisitor(dto.getVisitor());
        reservation.setName(dto.getName());
        reservation.setType(dto.getType());
        reservation.setMobile(dto.getMobile());
        reservation.setCreateBy(id+"");//必须要加上,否则会影响后面的查询预约列表的操作
        reservation.setUpdateBy(id+"");//必须要加上,否则会影响后面的查询预约列表的操作
        reservation.setStatus(0);//设置状态信息:待报道
        //搬砖结束
        this.save(reservation);//向数据库中添加数据
    }

    /**
     * 分页查询预约信息
     * @param status
     * @return
     */

    @Override
    public List<Reservation> pageList(Integer status) {
        //拿到当前用户id
        Long id=UserThreadLocal.get();
        return reservationMapper.pageList(status,id);
    }
    /**
     * 取消预约
     * @param id
     * @return
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = getById(id);
        if(ObjectUtil.isEmpty(reservation)){
            throw new RuntimeException("该条预约信息不存在");
        }
        reservation.setStatus(2);//将状态设置为2
        updateById(reservation);
    }
}
